package com.lj.demo.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lj.demo.eduservice.entity.*;
import com.lj.demo.eduservice.entity.vo.CourseQueryVo;
import com.lj.demo.eduservice.entity.vo.CourseWebVo;
import com.lj.demo.eduservice.mapper.EduCourseMapper;
import com.lj.demo.eduservice.service.EduChapterService;
import com.lj.demo.eduservice.service.EduCourseDescriptionService;
import com.lj.demo.eduservice.service.EduCourseService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lj.demo.eduservice.service.EduVideoService;
import com.lj.demo.servicebase.exception.AException;

import org.apache.poi.util.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author liujiang
 * @since 2020-12-03
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService eduCourseDescriptionService;

    //注入章节小节service层
    @Autowired
    private EduChapterService eduChapterService;

    @Autowired
    private EduVideoService eduVideoService;

    @Override
    public String addCourse(CourseInfoForm courseInfoForm) {
        if (StringUtils.isEmpty(courseInfoForm.getSubjectId()) || StringUtils.isEmpty(courseInfoForm.getDescription())){
            throw  new AException(20001,"请输入正确的信息");
        }
        EduCourse eduCourse = new EduCourse();
        //将courseInfoForm部分属性添加到eduCourse
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if (insert == 0){
            throw new AException(20001,"课程信息添加失败");
        }
        //返回课程id
        String cid = eduCourse.getId();
        //将课程描述添加到课程描述类中
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(cid);
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        boolean save = eduCourseDescriptionService.save(eduCourseDescription);
        if (!save){
            throw new AException(20001,"课程信息描述添加失败");
        }
        return cid;
    }

    //根据id查询课程列表
    @Override
    public CourseInfoForm getAllById(String id) {
        CourseInfoForm courseInfoForm = new CourseInfoForm();
        //根据id查询EduCourse中的数据
        EduCourse eduCourse = baseMapper.selectById(id);
        //将eduCourse中的数据赋值给CourseInfoForm
        if (eduCourse != null) {
            BeanUtils.copyProperties(eduCourse, courseInfoForm);
        }
        //根据id查询Description值
        EduCourseDescription eduCourseDescription = eduCourseDescriptionService.getById(id);
        if (eduCourseDescription != null){
            courseInfoForm.setDescription(eduCourseDescription.getDescription());
        }
        return courseInfoForm;
    }

    @Override
    public String updateCouAndDesc(CourseInfoForm courseInfoForm) {
        //修改课程显示表
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoForm,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if (update == 0){
            throw new AException(20001,"修改信息失败");
        }
        //修改描述表
        EduCourseDescription eduCourseDescription = new EduCourseDescription();
        eduCourseDescription.setId(courseInfoForm.getId());
        eduCourseDescription.setDescription(courseInfoForm.getDescription());
        boolean b = eduCourseDescriptionService.updateById(eduCourseDescription);
        if (!b){
            throw new AException(20001,"修改描述失败");
        }
        String id = courseInfoForm.getId();
         return id;

    }

    @Override
    public CoursePublishVo getCoursePubishById(String id) {
        CoursePublishVo coursePublishVo = baseMapper.getCoursePubishById(id);
        return coursePublishVo;
    }

    @Override
    public void publishCourse(String id) {
        EduCourse eduCourse = new EduCourse();
        eduCourse.setId(id);
        eduCourse.setStatus("Normal");
        Integer count = baseMapper.updateById(eduCourse);
    }

    //课程列表分页查询
    @Override
    public void pageQuery(Page<EduCourse> coursePage, CourseQuery courseQuery) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.orderByDesc("gmt_create");
        if (courseQuery == null) {
            baseMapper.selectPage(coursePage,queryWrapper);
            return;
        }
        String title = courseQuery.getTitle();
        String subjectId = courseQuery.getSubjectId();
        String subjectParentId = courseQuery.getSubjectParentId();
        String teacherId = courseQuery.getTeacherId();

        if (!StringUtils.isEmpty(title)){
            queryWrapper.like("title",title);
        }
        if (!StringUtils.isEmpty(teacherId)){
            queryWrapper.eq("teacher_id",teacherId);
        }
        if (!StringUtils.isEmpty(subjectParentId)){
            queryWrapper.ge("subject_parent_id",subjectParentId);
        }
        if (!StringUtils.isEmpty(subjectId)){
            queryWrapper.ge("subject_id",subjectId);
        }
        baseMapper.selectPage(coursePage,queryWrapper);
    }

    //根据课程id删除课程信息
    @Override
    public void deleteCourseQueryByCourseId(String courseId) {
        //删除小节信息
        eduVideoService.removeByCourseId(courseId);
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("course_id",courseId);
        //删除章节信息
        eduChapterService.remove(queryWrapper);
        //删除描述信息
         eduCourseDescriptionService.removeById(courseId);
        //删除课程信息
        int i = baseMapper.deleteById(courseId);
        if (i == 0){
            throw new AException(20001,"删除失败，请联系管理员");
        }
    }

    //根据讲师id查询这个讲师的课程列表
    @Override
    public List<EduCourse> getCourseList(String id) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper();
        queryWrapper.eq("teacher_id",id);
        //按照最后的更新的时间排序
        queryWrapper.orderByDesc("gmt_modified");
        List<EduCourse> courses = baseMapper.selectList(queryWrapper);
        return courses;
    }

    //根据条件分页查询课程列表
    @Override
    public Map<String, Object> getCourseWeb(Page<EduCourse> pageCourse, CourseQueryVo courseQuery) {
        QueryWrapper<EduCourse> queryWrapper = new QueryWrapper();

        if (!StringUtils.isEmpty(courseQuery.getSubjectParentId())){
            queryWrapper.eq("subject_parent_id", courseQuery.getSubjectParentId());
        }
        if (!StringUtils.isEmpty(courseQuery.getSubjectId())) {
            queryWrapper.eq("subject_id", courseQuery.getSubjectId());
        }

        if (!StringUtils.isEmpty(courseQuery.getBuyCountSort())) {
            queryWrapper.orderByDesc("buy_count");
        }

        if (!StringUtils.isEmpty(courseQuery.getGmtCreateSort())) {
            queryWrapper.orderByDesc("gmt_create");
        }

        if (!StringUtils.isEmpty(courseQuery.getPriceSort())) {
            queryWrapper.orderByDesc("price");
        }
        baseMapper.selectPage(pageCourse,queryWrapper);

        List<EduCourse> records = pageCourse.getRecords();
        long current = pageCourse.getCurrent();
        long pages = pageCourse.getPages();
        long size = pageCourse.getSize();
        long total = pageCourse.getTotal();
        boolean hasNext = pageCourse.hasNext();
        boolean hasPrevious = pageCourse.hasPrevious();

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("items", records);
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo selectInfoWebById(String courseId) {
        this.updatePageViewCount(courseId);
        return baseMapper.selectInfoWebById(courseId);
    }

    /**
     * 更新课程浏览数
     * @param id
     */
    @Override
    public void updatePageViewCount(String id) {
        EduCourse course = baseMapper.selectById(id);
        course.setViewCount(course.getViewCount() + 1);
        baseMapper.updateById(course);
        
    }


}
